### -*- coding: utf-8 -*- #############################################
# Разработано Паэглит Арвидом (http://arvid.dp.ua/)
# Все права защищены, 2007
#
# Developed by Paeglit Arvid (http://arvid.dp.ua/)
# All right reserved, 2007
#######################################################################
"""ActivationView class

$Id: activationview.py 40 2007-12-11 17:09:25Z Arvi3D $
"""
__author__  = "Arvid"
__license__ = "GPL"
__version__ = "$Revision: 40 $"
__date__ = "$Date: 2007-12-11 17:09:25 +0000 (Tue, 11 Dec 2007) $"

from zope.interface import Interface
from zope.schema import getFieldNamesInOrder
from zope.app.i18n import ZopeMessageFactory as _
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.app import zapi
from zope.publisher.interfaces import BadRequest

from datetime import datetime
import zlib

from arvidcontent.principalsource.interfaces import IMountUser
from settings import decompress

from logging import getLogger

logger = getLogger('useraddmail')

class ActivationView(object) :

    template = ViewPageTemplateFile('activation.pt')

    def activate(self, uniq = '', **kw):
        if not uniq:
            raise BadRequest(_(u"There is no unique key for activation"))

        try:
            userName, t = decompress(uniq)
        except (zlib.error, TypeError, ValueError, EOFError):
            logger.warning('Error reading unique key for activation', exc_info=True)
            raise BadRequest(_(u"Error reading unique key for activation"))

        userstorage = zapi.getUtility(IMountUser, context = self.context)

        user = userstorage.get(userName, None)
        if user is None:
            logger.warning('User had been stolen : %s' % userName, exc_info=True)
            raise BadRequest(_(u"User had been stolen. Please, register yourself one more time."))

        user.auth_istemp = False
        return self.template(**kw)
